package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //查询客户的所有状态到页面
        /*List<SystemDictionaryItem> intentionDegrees = systemDictionaryItemService.selectBySystemDictionaryIntentionDegree();
        model.addAttribute("intentionDegrees",intentionDegrees);*/
        HashMap<String, String> map = new HashMap<>();
        map.put("0","潜在客户");
        map.put("1","正式客户");
        map.put("2","开发失败客户");
        map.put("3","流失客户");
        map.put("4","客户池");
        model.addAttribute("map",map);
        //查询所有销售员共享到页面
        List<Employee> employees = employeeService.selectByRole("Market", "Market_Manager");
        model.addAttribute("employees",employees);

        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/customer/list";
    }

    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //潜在客户页面,只查询状态为潜在客户的数据,按照录入时间倒序排序
        qo.setStatus(Customer.STATUS_COMMON);
        //如果登录的员工是管理员或者经理,就可以看所有的客户,如果只是普通的销售人员,只能看自己的客户,不能看到别人的
        Subject subject = SecurityUtils.getSubject();
        Employee employee =(Employee) subject.getPrincipal();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            qo.setSellerId(employee.getId());
        }
        //销售人员下拉框,只查询有Market角色或者Market_Manager角色的员工
        //通过角色来查询员工分享到页面
        List<Employee> employees = employeeService.selectByRole("Market", "Market_Manager");
        model.addAttribute("employees",employees);

        //通过字典目录和明细查询客户job和来源,共享到页面
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionaryJob();
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySource();
        model.addAttribute("jobs",jobs);
        model.addAttribute("sources",sources);

        //通过字典目录和明细查询客户跟进方式,共享到页面
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectBySystemDictionaryCommunicationMethod();
        model.addAttribute("ccts",ccts);

        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/customer/potentialList";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if(customer.getId() != null){
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }

    //检查手机号是否重复
    @RequestMapping("/checkTel")
    @ResponseBody
    public Map<String,Boolean> checkName(String tel,Long id){
        Map<String,Boolean> map = new HashMap<>();
        Customer customer = customerService.selectByTel(tel);
        if(customer != null){
            if(id == customer.getId()){
                map.put("valid",true);
                return map;
            }
        }
        map.put("valid",customer == null);
        return map;
    }
}
